package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.NodeMapper;
import com.heima.pojo.dto.NodePageQuery;
import com.heima.pojo.domain.Node;
import com.heima.pojo.domain.NodePageVo;
import com.heima.pojo.domain.PageResult;
import com.heima.pojo.vo.VmNodeVo;
import com.heima.service.NodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NodeServiceImpl extends ServiceImpl<NodeMapper,Node> implements NodeService {

    @Autowired
    private NodeMapper nodeMapper;

    @Override
    public PageResult<NodePageVo> search(NodePageQuery npq) {
        //1.使用MyBatis-Plus获取分页信息(总记录数,总页数)
        Page<Node> page = new Page<>(npq.getPageIndex(), npq.getPageSize());
        LambdaQueryWrapper<Node> wrapper = Wrappers.<Node>lambdaQuery().eq(npq.getRegionId() != null, Node::getRegionId, npq.getRegionId())
                .like(npq.getName() != null, Node::getName, npq.getName());
        nodeMapper.selectPage(page, wrapper);

        //2.自定义查询获取NodePageVo数据
        // 假设 pageSize 是每页大小，pageIndex 是当前页码
        int offset = (npq.getPageIndex() - 1) *npq.getPageSize();


        List<NodePageVo> list = nodeMapper.selNodePageVo(offset,npq.getPageSize(),npq.getRegionId(),npq.getName());

        return new PageResult<>(npq.getPageIndex(),
                                npq.getPageSize(),
                                page.getPages(),
                                page.getTotal(),
                                 list);
    }

    /**
     * 获取点位详情
     * @return
     */

    @Override
    public List<VmNodeVo> getVmList(String nodeId) {
        List<VmNodeVo> vmNodeVo = nodeMapper.getVmList(nodeId);
        return vmNodeVo;
    }
}
